﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using StickTops.Properties;

namespace StickTops
{
    public partial class SettingsForm : Form
    {
        private static NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();

        public SettingsForm()
        {
            InitializeComponent();
            boxSaveToSql.Checked = Settings.Default.SaveToSql;
            boxAutoLogon.Checked = Settings.Default.AutoLogin;
            boxSaveUser.Checked = Settings.Default.SaveUsername;
            boxSavePass.Checked = Settings.Default.SavePassword;
            boxVertScroll.Checked = Settings.Default.MainTextVertScroll;
            boxHorizScroll.Checked = Settings.Default.MaintTextHorizScroll;
            boxMainTextPadding.Checked = Settings.Default.MainTextPadding;
            txtSaveInterval.Text = (Settings.Default.AutoSaveIntervall / 60000).ToString();
            boxStartOnBoot.Checked = GetStartOnBoot();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                int SaveInterval;

                if (ValidateAutoSaveIntervall(txtSaveInterval.Text,out SaveInterval) == false)
                {
                    MessageBox.Show("Sorry, Only enter a number from 1 to 35000 an an interval", "Invalid autosave interval",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                }
                else
                {

                    Settings.Default.AutoSaveIntervall = SaveInterval;
                    SetStartOnBoot(boxStartOnBoot.Checked);
                    Settings.Default.SaveToSql = boxSaveToSql.Checked;
                    Settings.Default.AutoLogin = boxAutoLogon.Checked;
                    Settings.Default.SaveUsername = boxSaveUser.Checked;
                    Settings.Default.SavePassword = boxSavePass.Checked;
                    Settings.Default.MainTextPadding = boxMainTextPadding.Checked;
                    Settings.Default.MainTextVertScroll = boxVertScroll.Checked;
                    Settings.Default.MaintTextHorizScroll = boxHorizScroll.Checked;
                    
            this.Close();
                }
            }

            catch (Exception ex) { log.Error("btnSaveSettings-Exception: " + ex.Message); }



        }

        private void SetStartOnBoot(bool make)
        {
            try
            {
                if (make)
                {
                    RegistryKey rkApp = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
                    rkApp.SetValue("StickyNotes", Application.ExecutablePath.ToString());
                    rkApp.Flush();
                    rkApp.Close();
                }

                else
                {
                    RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
                    rkApp.DeleteValue("StickyNotes", false);
                    rkApp.Flush();
                    rkApp.Close();
                }
            }

            catch (Exception e) { log.Error("SetStartOnBoot-Exception: "+e.Message); }

        }
        private bool GetStartOnBoot()
        {
            try
            {
                    RegistryKey rkApp = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
                    if (rkApp.GetValue("StickyNotes") == null) { return false; } else { return true; }
            }

            catch (Exception e) { log.Error("GetStartOnBoot-Exception: " + e.Message); }
            return false;
        }

        private bool ValidateAutoSaveIntervall(string InTime,out int result)
        {
            result = 0;
            try
            {
                int final = Int16.Parse(InTime);
                result = final * 1000 * 60;
            }
            catch (Exception e)
            {
                log.Error("ValidateAutoSaveIntervall-Exception: " + e.Message);
                return false;
            }
            return true;

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void boxAutoLogon_CheckedChanged(object sender, EventArgs e)
        {
            if (boxAutoLogon.Checked)
            {
                boxSaveUser.Checked = true; boxSaveUser.Enabled = false;
                boxSavePass.Checked = true; boxSavePass.Enabled = false;
            }
            else
            {

                boxSaveUser.Enabled = true; boxSaveUser.Checked = true; 
                boxSavePass.Enabled = true; boxSavePass.Checked = false; 
            }
        }

        private void btnDeleteUsers_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.Username = "";
            Properties.Settings.Default.Password = "";
            label2.Visible = true;
        }




    }
}
